package com.find.answer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.find.answer.domain.common.BaseContextHolder;
import com.find.answer.domain.dto.OrdersDto;
import com.find.answer.domain.dto.PageDto;
import com.find.answer.domain.http.Result;
import com.find.answer.domain.po.Orders;
import com.find.answer.domain.po.ShoppingCart;
import com.find.answer.service.OrdersService;
import com.find.answer.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Seon
 * @date 2022/11/5 11:34
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    /*
    订单的五种状态，分别是：1待付款，2待派送，3已派送，4已完成，5已取消
     */
    private static final Integer TO_BE_PAID = 1;
    private static final Integer TO_BE_DELIVERED = 2;
    private static final Integer DISPATCHED = 3;
    private static final Integer COMPLETED = 4;
    private static final Integer CANCELED = 5;

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public Result<?> submitOrder(@RequestBody OrdersDto ordersDto) {
        /*
        先提交订单，然后再删除购物车的所有数据。
         */
        ordersService.submitOrdersWithDish(ordersDto);
        ordersService.deleteShoppingCart();
        return Result.success("添加成功");
    }

    @GetMapping("/userPage")
    public Result<?> getUserPage(PageDto pageDto) {
        Page<OrdersDto> page = ordersService.getOrdersPage(pageDto);
        if (null == page) {
            return Result.error("暂无订单数据");
        } else {
            return Result.success(page);
        }
    }

    @GetMapping
    public Result<?> getOrderPage(PageDto pageDto,
                                  @RequestParam(value = "beginTime", required = false) String beginTime,
                                  @RequestParam(value = "endTime", required = false) String endTime,
                                  @RequestParam(value = "number", required = false) String number) {

        Page<OrdersDto> pageCondition = ordersService.getPageCondition(pageDto, beginTime, endTime, number);
        return Result.success(pageCondition);
    }
}
